package cn.dansj.utils.minio;

import java.util.Arrays;

public enum MinioBucketPolicy {
    readOnly("只读", Policy.readOnly),
    writeOnly("只写", Policy.writeOnly),
    readWrite("读写", Policy.readWrite),
    forbidVisit("禁止访问", Policy.forbidVisit);

    private final String policy;
    private final String name;

    MinioBucketPolicy(String name, String policy) {
        this.name = name;
        this.policy = policy;
    }

    public String getPolicy(String bucket) {
        return policy.replace(Policy.bucketParam, bucket);
    }

    public String getName() {
        return name;
    }

    public static MinioBucketPolicy get(String name) {
        for (MinioBucketPolicy minioBucketPolicy : MinioBucketPolicy.values()) {
            if (minioBucketPolicy.getName().equals(name)) {
                return minioBucketPolicy;
            }
        }
        return MinioBucketPolicy.forbidVisit;
    }

    public static MinioBucketPolicy get(String bucket, String policy) {
        String[] splitPolicy = policy.replaceAll("\\s+", "").split("[{}\\[\\],\n]");
        Arrays.sort(splitPolicy);

        for (MinioBucketPolicy minioBucketPolicy : MinioBucketPolicy.values()) {
            String[] splitInnerPolicy = minioBucketPolicy.getPolicy(bucket).replaceAll("\\s+", "").split("[{}\\[\\],\n]");
            Arrays.sort(splitInnerPolicy);
            if (Arrays.equals(splitPolicy, splitInnerPolicy)) {
                return minioBucketPolicy;
            }
        }
        return MinioBucketPolicy.forbidVisit;
    }

    private static class Policy {
        /**
         * 桶占位符
         */
        public static final String bucketParam = "${bucket}";
        /**
         * bucket权限-只读
         */
        public static final String readOnly = "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\",\"Principal\":{\"AWS\":[\"*\"]},\"Action\":[\"s3:GetBucketLocation\",\"s3:ListBucket\"],\"Resource\":[\"arn:aws:s3:::" + bucketParam + "\"]},{\"Effect\":\"Allow\",\"Principal\":{\"AWS\":[\"*\"]},\"Action\":[\"s3:GetObject\"],\"Resource\":[\"arn:aws:s3:::" + bucketParam + "/*\"]}]}";
        /**
         * bucket权限-只读
         */
        public static final String writeOnly = "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\",\"Principal\":{\"AWS\":[\"*\"]},\"Action\":[\"s3:GetBucketLocation\",\"s3:ListBucketMultipartUploads\"],\"Resource\":[\"arn:aws:s3:::" + bucketParam + "\"]},{\"Effect\":\"Allow\",\"Principal\":{\"AWS\":[\"*\"]},\"Action\":[\"s3:AbortMultipartUpload\",\"s3:DeleteObject\",\"s3:ListMultipartUploadParts\",\"s3:PutObject\"],\"Resource\":[\"arn:aws:s3:::" + bucketParam + "/*\"]}]}";
        /**
         * bucket权限-读写
         */
        public static final String readWrite = "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\",\"Principal\":{\"AWS\":[\"*\"]},\"Action\":[\"s3:GetBucketLocation\",\"s3:ListBucket\",\"s3:ListBucketMultipartUploads\"],\"Resource\":[\"arn:aws:s3:::" + bucketParam + "\"]},{\"Effect\":\"Allow\",\"Principal\":{\"AWS\":[\"*\"]},\"Action\":[\"s3:DeleteObject\",\"s3:GetObject\",\"s3:ListMultipartUploadParts\",\"s3:PutObject\",\"s3:AbortMultipartUpload\"],\"Resource\":[\"arn:aws:s3:::" + bucketParam + "/*\"]}]}";

        public static final String forbidVisit = "{\"Version\":\"2012-10-17\",\"Statement\":[]}";
    }
}
